package com.dbkj.axb_a;

import com.alibaba.fastjson.JSONArray;
import com.dbkj.common.model.AxbA;
import com.dbkj.common.utils.RedisUtils;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Max
 * @Date: 2019/10/29 19:07
 */

public class AxbAService {

    private static final AxbA dao = new AxbA().dao();

    public Page<AxbA> paginate(Integer pageNumber, Integer pageSize, String telA,String aRelation,String gateway,String prefixA,String prefixX,String remark) {
        SqlPara sql = dao.getSqlPara("tela.show", Kv.by("telA",telA).set("aRelation",aRelation).set("gateway",gateway).set("prefixA",prefixA).set("prefixX",prefixX).set("remark",remark));
        return dao.paginate(pageNumber,pageSize,sql);
    }

    public void delete(Integer id) {
        AxbA a = dao.findById(id);
        a.delete();
        if(PropKit.getBoolean("is_cluster")){
            RedisUtils.cluster.del("tela_"+a.getTelA()+"_concurrent_list");
        }else{
            RedisUtils.jedis.del("tela_"+a.getTelA()+"_concurrent_list");
        }
    }

    public List<List<String>> createListStringHead() {
        List<List<String>> head = new ArrayList<>();
        List<String> headCoulumn1 = new ArrayList<>();
        List<String> headCoulumn2 = new ArrayList<>();
        List<String> headCoulumn3 = new ArrayList<>();
        List<String> headCoulumn4 = new ArrayList<>();
        List<String> headCoulumn5 = new ArrayList<>();
        List<String> headCoulumn6 = new ArrayList<>();
        List<String> headCoulumn7 = new ArrayList<>();
        List<String> headCoulumn8 = new ArrayList<>();
        List<String> headCoulumn9 = new ArrayList<>();
        headCoulumn1.add("A号码");
        headCoulumn2.add("a号前缀");
        headCoulumn3.add("x号前缀");
        headCoulumn4.add("网关");
        headCoulumn5.add("每天并发数");
        headCoulumn6.add("时刻并发数");
        headCoulumn7.add("小时并发数");
        headCoulumn8.add("归属组名");
        headCoulumn9.add("备注");
        head.add(headCoulumn1);
        head.add(headCoulumn2);
        head.add(headCoulumn3);
        head.add(headCoulumn4);
        head.add(headCoulumn5);
        head.add(headCoulumn6);
        head.add(headCoulumn7);
        head.add(headCoulumn8);
        head.add(headCoulumn9);
        return head;
    }

    public void deleteBatch(JSONArray ids) {
        for(int i=0;i<ids.size();i++){
            AxbA a = dao.findById(ids.getInteger(i));
            a.delete();
            if(PropKit.getBoolean("is_cluster")){
                RedisUtils.cluster.del("tela_"+a.getTelA()+"_concurrent_list");
            }else{
                RedisUtils.jedis.del("tela_"+a.getTelA()+"_concurrent_list");
            }
        }
    }

    public void updateBatch(String telA, String aRelation, String gateway, String prefixA, String prefixX, String remark, String key, String value) {
        if(StrKit.isBlank(telA) && StrKit.isBlank(aRelation) && StrKit.isBlank(gateway) && StrKit.isBlank(prefixA) && StrKit.isBlank(prefixX) && StrKit.isBlank(remark)){
            return;
        }
        SqlPara sql = dao.getSqlPara("tela.updateBatch",Kv.by("tel_a",telA)
                .set("a_relation",aRelation)
                .set("gateway",gateway)
                .set("prefix_a",prefixA)
                .set("prefix_x",prefixX)
                .set("remark",remark)
                .set("key",key)
                .set("value",value));

        Db.update(sql);
    }

    /**
     *
     * @param id 组的ID
     * @return
     */
    public boolean hasRelation(Integer id){
        AxbA a = dao.findFirst("select * from axb_a where relation_id = ?",id);
        if(a == null){
            return false;
        } else {
            return true;
        }
    }
}
